using NPOI.SS.UserModel;
using Publicuse.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Export
{
    /// <summary>
    /// 导出数据到Excel表格辅助类
    /// </summary>
    public class ExportDataExport<T>
    {
        private static Dictionary<string,T> exportData = null;

        private static ExportDataExport<T> exportCBLL = null;

        private ExportDataExport()
        {

        }

        /// <summary>
        /// 实例化
        /// </summary>
        static ExportDataExport()
        {
            exportCBLL = new ExportDataExport<T>();
            exportData = new Dictionary<string,T>();
        }

        /// <summary>
        /// 获取导出实例
        /// </summary>
        public static ExportDataExport<T> GetExportCBLL
        {
            get { return exportCBLL; }
        }

        private static readonly object lockkey = new object();
        /// <summary>
        /// 添加要导出的数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        public void AddExportData(string key, T t)
        {
            lock(lockkey)
            {
                if (exportData.ContainsKey(key))
                {
                    exportData.Remove(key);
                    exportData.Add(key, t);
                }
                else
                {
                    exportData.Add(key, t);
                }
            }
        }

        /// <summary>
        /// 获取要导出的数据
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetExportData(string key)
        {
            T t = default(T);
            exportData.TryGetValue(key, out t);
            return t;
        }

        /// <summary>
        /// 清空所有导出数据
        /// </summary>
        public void ClearExportData()
        {
            exportData.Clear();
        }

        /// <summary>
        /// 初始化导出
        /// </summary>
        /// <param name="action"></param>
        public void InitExportData(Action<ExportDataExport<T>> action)
        {
            var ins = ExportDataExport<T>.GetExportCBLL;
            Task.Run(() =>
            {
              action.Invoke(ins);
            });
        }
    }
}
